﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace H
{
	// Token: 0x02000212 RID: 530
	public class MixController : H_InputBase, IBeginDragHandler, IDragHandler, IEndDragHandler, IPointerClickHandler, IScrollHandler, IEventSystemHandler
	{
		// Token: 0x17000176 RID: 374
		// (get) Token: 0x06000E88 RID: 3720 RVA: 0x0005AD98 File Offset: 0x00058F98
		// (set) Token: 0x06000E89 RID: 3721 RVA: 0x0005ADA0 File Offset: 0x00058FA0
		public MixController.MODE mode { get; private set; }

		// Token: 0x06000E8A RID: 3722 RVA: 0x0005ADAC File Offset: 0x00058FAC
		private void Start()
		{
			this.mode = MixController.MODE.MANUAL;
			this.slider = this.sliderRoot.GetComponentInChildren<Slider>();
			this.hitArea = new HitArea(this.hitAreaImg, this.area);
			this.areaEcho = new AreaEcho(this.echoImg, this.area);
		}

		// Token: 0x06000E8B RID: 3723 RVA: 0x0005AE00 File Offset: 0x00059000
		public void ChangeStyle()
		{
			this.hitArea.Setup(this.h_scene.mainMembers.HasHitArea());
		}

		// Token: 0x06000E8C RID: 3724 RVA: 0x0005AE20 File Offset: 0x00059020
		protected override void ChangeState(H_STATE state)
		{
			base.ChangeState(state);
			string text = string.Empty;
			this.pose = 1f;
			this.stroke = -1f;
			Vector2 anchoredPosition = new Vector2(this.pose * this.area.rect.xMax, this.stroke * this.area.rect.yMax);
			anchoredPosition.x = Mathf.Clamp(anchoredPosition.x, this.area.rect.xMin, this.area.rect.xMax);
			anchoredPosition.y = Mathf.Clamp(anchoredPosition.y, this.area.rect.yMin, this.area.rect.yMax);
			this.pointImg.rectTransform.anchoredPosition = anchoredPosition;
			this.autoMode.StartWander(new Vector2(this.pose, this.stroke), this.AutoGageVal(), this.autoDistanceThreshold);
			if (state == H_STATE.PRE_INSERT_WAIT)
			{
				text = "クリック\nで\n挿入";
			}
			else if (state == H_STATE.PRE_TOUCH_WAIT)
			{
				if (this.members.StyleData.type == H_StyleData.TYPE.PETTING)
				{
					text = "クリック\nで\n愛撫開始";
				}
				else
				{
					text = "クリック\nで\n奉仕開始";
				}
			}
			else if (state == H_STATE.INSERTED_WAIT)
			{
				text = "クリック\nで\nピストン開始";
			}
			else if (state == H_STATE.LOOP)
			{
				text = "ドラッグ\nで\n姿勢変更";
			}
			else if (state == H_STATE.IN_XTC_AFTER_WAIT)
			{
				text = "クリック\nで\n再開";
			}
			else if (state == H_STATE.OUT_XTC_AFTER_WAIT)
			{
				if (this.members.StyleData.type == H_StyleData.TYPE.INSERT)
				{
					text = "クリック\nで\n再挿入";
				}
				else
				{
					text = "クリック\nで\n再開";
				}
			}
			else if (state == H_STATE.EXTRACTED_WAIT || state == H_STATE.COUGH_WAIT || state == H_STATE.VOMIT_WAIT || state == H_STATE.DRINK_WAIT)
			{
				text = "クリック\nで\n再挿入";
			}
			this.dragEnable = (state == H_STATE.LOOP);
			this.SetText(text);
			if (this.mode == MixController.MODE.FULL_AUTO && (state == H_STATE.PRE_INSERT_WAIT || state == H_STATE.PRE_TOUCH_WAIT || state == H_STATE.INSERTED_WAIT || state == H_STATE.LOOP))
			{
				this.text.enabled = false;
			}
		}

		// Token: 0x06000E8D RID: 3725 RVA: 0x0005B054 File Offset: 0x00059254
		public override void Update()
		{
			base.Update();
			if (this.members == null)
			{
				return;
			}
			for (int i = 0; i < this.toggles.Length; i++)
			{
				if (this.toggles[i].isOn && this.mode != (MixController.MODE)i)
				{
					this.ChangeMode((MixController.MODE)i);
					break;
				}
			}
			if (this.isDragging && !Input.GetMouseButton(0))
			{
				this.isDragging = false;
			}
			if (!this.dragEnable)
			{
				this.isDragging = false;
			}
			if (this.mode == MixController.MODE.FULL_AUTO && this.dragEnable)
			{
				float speed = this.h_scene.mainMembers.GetFemale(0).body.Anime.speed;
				this.autoMode.Update(speed, this.autoChangeSpeed, this.AutoGageVal(), this.autoDistanceThreshold);
				this.pose = this.autoMode.nowPos.x;
				this.stroke = this.autoMode.nowPos.y;
				Vector2 anchoredPosition = new Vector2(this.pose * this.area.rect.xMax, this.stroke * this.area.rect.yMax);
				anchoredPosition.x = Mathf.Clamp(anchoredPosition.x, this.area.rect.xMin, this.area.rect.xMax);
				anchoredPosition.y = Mathf.Clamp(anchoredPosition.y, this.area.rect.yMin, this.area.rect.yMax);
				this.pointImg.rectTransform.anchoredPosition = anchoredPosition;
			}
			else if (this.isDragging)
			{
				Vector2 vector = Vector3.zero;
				vector.x = Input.GetAxis("Mouse X");
				vector.y = Input.GetAxis("Mouse Y");
				vector *= this.moveRate;
				Vector2 anchoredPosition2 = this.pointImg.rectTransform.anchoredPosition + vector;
				anchoredPosition2.x = Mathf.Clamp(anchoredPosition2.x, this.area.rect.xMin, this.area.rect.xMax);
				anchoredPosition2.y = Mathf.Clamp(anchoredPosition2.y, this.area.rect.yMin, this.area.rect.yMax);
				this.pointImg.rectTransform.anchoredPosition = anchoredPosition2;
				float t = Mathf.InverseLerp(this.area.rect.xMin, this.area.rect.xMax, anchoredPosition2.x);
				float t2 = Mathf.InverseLerp(this.area.rect.yMin, this.area.rect.yMax, anchoredPosition2.y);
				this.pose = Mathf.Lerp(-1f, 1f, t);
				this.stroke = Mathf.Lerp(-1f, 1f, t2);
				GameCursor.Lock();
			}
			if (this.mode == MixController.MODE.MANUAL)
			{
				bool interactable = this.slider.interactable;
				bool flag = EventSystem.current && EventSystem.current.IsPointerOverGameObject();
				if (this.slider.image.canvas != null && this.slider.image.canvas.enabled && !flag)
				{
					this.slider.value += Input.mouseScrollDelta.y * this.wheelSpeed;
				}
			}
			else
			{
				this.slider.value = this.stroke;
			}
			this.pointImg.enabled = this.dragEnable;
			this.members.param.hit = false;
			this.members.param.hitEnableStyle = this.hitArea.Enable;
			if (this.dragEnable)
			{
				if (!this.areaEcho.show && this.hitArea.Check(new Vector2(this.pose, this.stroke)))
				{
					this.areaEcho.Set(this.pointImg.rectTransform.anchoredPosition);
				}
				this.members.param.hit = this.areaEcho.show;
			}
			if (this.areaEcho.show)
			{
				this.areaEcho.Update();
			}
			this.hitArea.SetShow(this.dragEnable && GlobalData.PlayData.unlockShowHitArea);
		}

		// Token: 0x06000E8E RID: 3726 RVA: 0x0005B55C File Offset: 0x0005975C
		public void OnPointerClick(PointerEventData eventData)
		{
			if (this.isDragging)
			{
				return;
			}
			this.h_scene.OnInput(H_INPUT.CLICK_PAD);
		}

		// Token: 0x06000E8F RID: 3727 RVA: 0x0005B578 File Offset: 0x00059778
		public void OnBeginDrag(PointerEventData eventData)
		{
			if (this.dragEnable)
			{
				this.isDragging = true;
				GameCursor.Lock();
			}
		}

		// Token: 0x06000E90 RID: 3728 RVA: 0x0005B594 File Offset: 0x00059794
		public void OnDrag(PointerEventData eventData)
		{
			if (this.dragEnable)
			{
				GameCursor.Lock();
				this.isDragging = true;
			}
		}

		// Token: 0x06000E91 RID: 3729 RVA: 0x0005B5B0 File Offset: 0x000597B0
		public void OnEndDrag(PointerEventData eventData)
		{
			this.isDragging = false;
		}

		// Token: 0x06000E92 RID: 3730 RVA: 0x0005B5BC File Offset: 0x000597BC
		private void SetText(string str)
		{
			this.text.enabled = (str != null && str.Length > 0);
			this.text.text = str;
		}

		// Token: 0x06000E93 RID: 3731 RVA: 0x0005B5E8 File Offset: 0x000597E8
		public void ChangeSliderSpeed(float val)
		{
			this.speed = val;
		}

		// Token: 0x06000E94 RID: 3732 RVA: 0x0005B5F4 File Offset: 0x000597F4
		public void ChangeMode(MixController.MODE newMode)
		{
			this.mode = newMode;
			this.sliderRoot.SetActive(this.mode == MixController.MODE.MANUAL);
			if (newMode == MixController.MODE.FULL_AUTO)
			{
				this.autoMode.StartWander(new Vector2(this.pose, this.stroke), this.AutoGageVal(), this.autoDistanceThreshold);
				this.text.enabled = !this.dragEnable;
			}
			else
			{
				this.text.enabled = true;
			}
		}

		// Token: 0x06000E95 RID: 3733 RVA: 0x0005B670 File Offset: 0x00059870
		public void OnScroll(PointerEventData eventData)
		{
			if (this.mode == MixController.MODE.MANUAL)
			{
				bool interactable = this.slider.interactable;
				if (this.slider.image.canvas != null && this.slider.image.canvas.enabled)
				{
					this.slider.value += eventData.scrollDelta.y * this.wheelSpeed;
				}
			}
		}

		// Token: 0x06000E96 RID: 3734 RVA: 0x0005B6F0 File Offset: 0x000598F0
		private float AutoGageVal()
		{
			if (this.h_scene.mainMembers.StyleData != null && this.h_scene.mainMembers.StyleData.type != H_StyleData.TYPE.SERVICE)
			{
				return this.h_scene.mainMembers.FemaleGageVal;
			}
			return 0.5f;
		}

		// Token: 0x04000EF7 RID: 3831
		[SerializeField]
		private H_Scene h_scene;

		// Token: 0x04000EF8 RID: 3832
		[SerializeField]
		private RectTransform area;

		// Token: 0x04000EF9 RID: 3833
		[SerializeField]
		private Image pointImg;

		// Token: 0x04000EFA RID: 3834
		[SerializeField]
		private Image hitAreaImg;

		// Token: 0x04000EFB RID: 3835
		[SerializeField]
		private Image echoImg;

		// Token: 0x04000EFC RID: 3836
		[SerializeField]
		private Text text;

		// Token: 0x04000EFD RID: 3837
		[SerializeField]
		private float moveRate = 1f;

		// Token: 0x04000EFE RID: 3838
		[SerializeField]
		private GameObject sliderRoot;

		// Token: 0x04000EFF RID: 3839
		private Slider slider;

		// Token: 0x04000F00 RID: 3840
		[SerializeField]
		private float wheelSpeed = 0.1f;

		// Token: 0x04000F01 RID: 3841
		[SerializeField]
		private Toggle[] toggles;

		// Token: 0x04000F03 RID: 3843
		private bool isDragging;

		// Token: 0x04000F04 RID: 3844
		private AutoMode autoMode = new AutoMode();

		// Token: 0x04000F05 RID: 3845
		[Range(0f, 1f)]
		[SerializeField]
		private float autoChangeSpeed = 0.1f;

		// Token: 0x04000F06 RID: 3846
		[SerializeField]
		[Range(0f, 1f)]
		private float autoDistanceThreshold = 0.2f;

		// Token: 0x04000F07 RID: 3847
		private bool dragEnable;

		// Token: 0x04000F08 RID: 3848
		private HitArea hitArea;

		// Token: 0x04000F09 RID: 3849
		private AreaEcho areaEcho;

		// Token: 0x02000213 RID: 531
		public enum MODE
		{
			// Token: 0x04000F0B RID: 3851
			MANUAL,
			// Token: 0x04000F0C RID: 3852
			SEMI_AUTO,
			// Token: 0x04000F0D RID: 3853
			FULL_AUTO,
			// Token: 0x04000F0E RID: 3854
			NUM
		}
	}
}
